package com.qdxwx.common.exception;

import com.qdxwx.common.utils.MessageUtils;

/**
 * 基础异常
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
public class BaseException extends RuntimeException {

    private static final long serialVersionUID = 2876811117092954966L;
    /**
     * 所属模块
     */
    private final String module;

    /**
     * 错误码
     */
    private final String code;

    /**
     * 错误码对应的参数
     */
    private final Object[] args;


    public BaseException(String defaultMessage) {
        this(defaultMessage, null, null, null);
    }

    public BaseException(int code) {
        this(null, String.valueOf(code), null, null);
    }

    public BaseException(String module, String code) {
        this(module, code, null);
    }

    public BaseException(String module, String code, Object[] args) {
        this(module, code, args, null);
    }

    public BaseException(String module, String code, Object[] args, String defaultMessage) {
        super(defaultMessage);
        this.module = module;
        this.code = code;
        this.args = args;
    }

    @Override
    public String getMessage() {
        String message = "";
        if (module != null) {
            message += String.format("[module:%s]", module);
        }
        if (code != null) {
            message += MessageUtils.message(code, args);
        }
        if (message.equals("")) {
            return super.getMessage();
        }
        return message;
    }

    public String getModule() {
        return module;
    }

    public String getCode() {
        return code;
    }

    public Object[] getArgs() {
        return args;
    }
}
